package com.ncu.cloundeoffice.controller;


import com.ncu.cloundeoffice.pojo.Login;
import com.ncu.cloundeoffice.pojo.ResponseResult;
import com.ncu.cloundeoffice.pojo.TAdmin;
import com.ncu.cloundeoffice.service.TAdminService;
import com.ncu.cloundeoffice.service.TRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.security.Principal;

@RestController
public class LoginController {

    @Autowired
    private TAdminService tAdminService;

    @Autowired
    private TRoleService tRoleService;

    /**
     * 用户登录
     * @param login
     * @param request
     * @param session
     * @return
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody Login login, HttpServletRequest request, HttpSession session){
        String code = (String) session.getAttribute("code");
        if(!code.equals(login.getCode())){
            return ResponseResult.error("验证码填写不正确");
        }
        return tAdminService.login(login.getUsername(),login.getPassword(),request);

    }

    /**
     * 获取当前登录的用户信息
     * @param principal
     * @return
     */
    @GetMapping("/login/info")
    public TAdmin test1(Principal principal){
        if(principal == null){
            return null;
        }
        TAdmin tAdmin = tAdminService.getAdminByUsername(principal.getName());
        tAdmin.setTRoles(tRoleService.getTRoleByAdminId(tAdmin.getId()));
        return tAdmin;
    }

    /**
     * 注销登录
     * @return
     */
    @PostMapping("/logout")
    public ResponseResult logout(){

        return ResponseResult.success("注销成功");
    }

    @GetMapping("/system/cfg/aaa")
    public String aaa(){
        return "aaaa";
    }


    @GetMapping("/employee/advanced/ccc")
    public String bbb(){
        return "bbb";
    }
}
